package com.mioto.ld.module.alert.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.component.EditType;
import com.mioto.ld.module.alert.model.*;
import com.mioto.ld.module.alert.service.IAlertTriggerService;
import com.mioto.ld.module.device.service.IDeviceAlertLogService;
import com.mioto.ld.utils.BaseUtil;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.alert.dao.AlertConfigDao;
import com.mioto.ld.module.alert.service.IAlertConfigService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author mioto
 * @date 2024-05-20 23:49:39
 */
@Service("alertConfigService")
public class AlertConfigServiceImpl implements IAlertConfigService{

    @Resource
    private AlertConfigDao alertConfigDao;
    @Resource
    private IAlertTriggerService alertTriggerService;
    @Resource
    private IDeviceAlertLogService deviceAlertLogService;

    @Override
    public List<AlertConfig> findList(AlertConfig alertConfig) {
        return alertConfigDao.findList(alertConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(AlertConfigDTO alertConfig) {
        alertConfig.setStatus(AlertStatus.ALERT_STATUS_ENABLE);
        alertConfig.initCreateData();
        int result = alertConfigDao.insertIgnoreNull(alertConfig);
        if (CollUtil.isNotEmpty(alertConfig.getAlertTriggerDTOS())){
            alertConfig.getAlertTriggerDTOS().forEach(alertTriggerDTO -> {
                alertTriggerDTO.setAlertId(alertConfig.getId());
                alertTriggerDTO.setTriggerCondition(alertTriggerDTO.toJsonStr());
                alertTriggerService.insert(alertTriggerDTO);
            });
        }
        return result;
    }

    @Override
    public int update(AlertConfig alertConfig) {
        return alertConfigDao.update(alertConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateIgnoreNull(AlertConfigDTO alertConfig) {
        if (ObjectUtil.isNotNull(alertConfig.getId())){
            alertConfigDao.updateIgnoreNull(alertConfig);
        }
        if (CollUtil.isNotEmpty(alertConfig.getAlertTriggerDTOS())){
            alertConfig.getAlertTriggerDTOS().forEach(alertTriggerDTO -> {
                switch (alertTriggerDTO.getEditType()){
                    case EditType.EDIT_TYPE_DELETE:
                        alertTriggerService.deleteByColumn("id",alertTriggerDTO.getId());
                        break;
                    case EditType.EDIT_TYPE_INSERT:
                        alertTriggerDTO.setTriggerCondition(alertTriggerDTO.toJsonStr());
                        alertTriggerService.insert(alertTriggerDTO);
                        break;
                    case EditType.EDIT_TYPE_UPDATE:
                        alertTriggerDTO.setTriggerCondition(alertTriggerDTO.toJsonStr());
                        alertTriggerService.updateIgnoreNull(alertTriggerDTO);
                        break;
                }
            });
        }
        return 1;
    }

    @Override
    public AlertConfig findByColumn(String column, Object value) {
        return alertConfigDao.findByColumn(column,value);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByColumn(String column, Object value) {
        int result = alertConfigDao.deleteByColumn(column,value);
        alertTriggerService.deleteByColumn("alert_id",value);
        deviceAlertLogService.deleteByColumn("alert_id",value);
        return result;
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return alertConfigDao.batchDelete(ids);
    }

    @Override
    public List<AlertConfigPagerVO> pager(AlertConfig alertConfig, BasePager basePager) {
        int pageNo = 0;
        if (basePager.getPage() > 1){
            pageNo = (basePager.getPage() - 1)*basePager.getRows();
        }
        return alertConfigDao.pager(alertConfig,pageNo,basePager.getRows(), BaseUtil.buildUserFilterCondition());
    }

    @Override
    public int findCount(AlertConfig alertConfig) {
        return alertConfigDao.findCount(alertConfig, BaseUtil.buildUserFilterCondition());
    }

    @Override
    public AlertConfigDetailVO findDetail(int id) {
        return alertConfigDao.findDetail(id);
    }
}